
using System;
using System.Diagnostics;
using System.Windows;
using Microsoft.Devices.Sensors;
using Microsoft.Phone.Shell;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Tama_Chan.GameEngine_ns;
using Microsoft.Devices;


namespace Tama_Chan
{
   public class Game1 : Game
   {
      private const int BaselineScreenWidth = 480;
      private const int BaselineScreenHeight = 800;
      private const int BaselineHudHeight = 50;
      private const int Padding = 1;

      private Accelerometer Accelerometer { get; set; }
      private GraphicsDeviceManager Graphics { get; set; }
      private GameEngine GameEngine { get; set; }
      public Rectangle HudBounds { get; set; }
      public Rectangle GameBounds { get; private set; }
      public SpriteBatch SpriteBatch { get; private set; }
      public Vector2 AccelerometerDelta { get; private set; }
      public VibrateController VibrateController { get; set; }

		public float GlobalScale { get; set; }
		public Vector3 GlobalTransformation { get; set; }
   	public Matrix GlobalScaleMatrix { get; set; }


      public Game1()
      {
         Graphics = new GraphicsDeviceManager(this);
         Content.RootDirectory = "Content";
         TargetElapsedTime = TimeSpan.FromTicks(333333);

			Accelerometer = new Accelerometer();
			Accelerometer.CurrentValueChanged += AccelerometerReadingChanged;

			PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled;
			InactiveSleepTime = TimeSpan.FromSeconds(1);

			VibrateController = VibrateController.Default; 

			var deviceScreenWidth = Application.Current.Host.Content.ActualWidth;
			var deviceScreenHeight = Application.Current.Host.Content.ActualHeight;

			// Reverse width and height because the default for phone games is landscape
			Graphics.PreferredBackBufferWidth = (int)deviceScreenWidth;
			Graphics.PreferredBackBufferHeight = (int)deviceScreenHeight;

			// (X)Horizontal, (Y)Vertical, (Z)1: no change
			GlobalScale = Math.Min((float)deviceScreenWidth / BaselineScreenWidth,
										  (float)deviceScreenHeight / BaselineScreenHeight);
			GlobalTransformation = new Vector3(GlobalScale, GlobalScale, 1);
			GlobalScaleMatrix = Matrix.CreateScale(GlobalTransformation);

			HudBounds = new Rectangle(0, 0, BaselineScreenWidth, BaselineHudHeight);
			GameBounds = new Rectangle(Padding, HudBounds.Bottom + Padding, BaselineScreenWidth - Padding * 2, BaselineScreenHeight - HudBounds.Height - Padding * 3);

			GameEngine = new GameEngine(this);
		}


      protected override void Initialize()
		{
			AccelerometerDelta = Vector2.Zero;
			Accelerometer.Start();

         GameEngine.Initialize();
         base.Initialize();
      }


      protected override void LoadContent()
      {
         SpriteBatch = new SpriteBatch(GraphicsDevice);
         GameEngine.LoadContent();
      }


      protected override void UnloadContent()
      {
         // TODO: Unload any non ContentManager content here
      }


      protected override void Update(GameTime gameTime)
      {
			if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
			{
				switch (GameEngine.GameState)
				{
					case GameState.HomeMenuShowing:
						Exit();
						break;
					case GameState.InfoMenuShowing:
						GameEngine.GameState = GameState.HomeMenuShowing;
						break;
					case GameState.Playing:
						GameEngine.GameState = GameState.Paused;
						break;
					case GameState.Paused:
						GameEngine.GameState = GameState.Playing;
						break;
					case GameState.GameOver:
						GameEngine.GameState = GameState.HomeMenuShowing;
						break;
				}
			}

         GameEngine.Update(gameTime);
         base.Update(gameTime);
      }


      protected override void Draw(GameTime gameTime)
      {
         GameEngine.Draw();
         base.Draw(gameTime);
      }


		private void AccelerometerReadingChanged(object sender, SensorReadingEventArgs<AccelerometerReading> e)
      {
         // Game is portrait so flip y axis
         AccelerometerDelta = new Vector2(e.SensorReading.Acceleration.X, e.SensorReading.Acceleration.Y * -1.0f);

         if(float.IsNaN(AccelerometerDelta.X) || float.IsNaN(AccelerometerDelta.Y))
         {
            // It sitting still you don't get 0, you get NaN.
            AccelerometerDelta = Vector2.Zero;
         }
      }


      protected override void OnExiting(object sender, EventArgs args)
      {
         PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Enabled;
         base.OnExiting(sender, args);
      }

   }
}
